package cn.occgmat.service.impl;

import cn.occgmat.entity.RbsystemPassenger;
import cn.occgmat.entity.RbsystemUser;
import cn.occgmat.dao.RbsystemUserDao;
import cn.occgmat.service.RbsystemUserService;
import cn.occgmat.util.BaseResponse;
import cn.occgmat.util.StatusCode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (RbsystemUser)表服务实现类
 *
 * @author shimmer
 * @since 2021-03-27 16:09:10
 */
@Transactional(rollbackFor = Exception.class)
@Service("rbsystemUserService")
public class RbsystemUserServiceImpl implements RbsystemUserService {
    @Resource
    private RbsystemUserDao rbsystemUserDao;

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public RbsystemUser queryById(Integer userId) {
        return this.rbsystemUserDao.queryById(userId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    @Override
    public List<RbsystemUser> queryAllByLimit(int offset, int limit) {
        int startPage = (offset - 1) * limit;
        return this.rbsystemUserDao.queryAllByLimit(startPage, limit);
    }

    /**
     * 新增数据
     *
     * @param rbsystemUser 实例对象
     * @return 实例对象
     */
    @Override
    public RbsystemUser insert(RbsystemUser rbsystemUser) {
        this.rbsystemUserDao.insert(rbsystemUser);
        return rbsystemUser;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public int update(RbsystemUser user) {
        return this.rbsystemUserDao.update(user);
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer userId) {
        return this.rbsystemUserDao.deleteById(userId) > 0;
    }

    @Override
    public Integer getCount() {
        return this.rbsystemUserDao.getCount();
    }

    @Override
    public BaseResponse<RbsystemUser> login(RbsystemUser user) {
        List<RbsystemUser> userList = this.rbsystemUserDao.queryAll(user);
        BaseResponse<RbsystemUser> baseResponse;
        if (userList != null) {
            baseResponse = new BaseResponse<>(StatusCode.Success);
            baseResponse.setData(userList);
        } else {
            baseResponse = new BaseResponse<>(StatusCode.Fail);
        }
        return baseResponse;
    }

}
